<template>
  <div class="app-container">
    <el-form ref="employeeForm" :model="employeeForm" :rules="rules" label-width="100px" style="width: 36%">

      <div>
        <el-form-item label="姓名" required prop="name">
          <el-input v-model="employeeForm.name" maxlength="16"/>
        </el-form-item>

        <el-form-item label="性别" required prop="sex">
          <template>
            <el-radio v-model="employeeForm.sex" label="男">男</el-radio>
            <el-radio v-model="employeeForm.sex" label="女">女</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="年龄" required prop="age">
          <el-input v-model="employeeForm.age" type="number"/>
        </el-form-item>

        <el-form-item label="部门" required prop="departId">
          <el-select v-model="employeeForm.departId" placeholder="请选择部门">
            <el-option label="皇室" value="1"/>
            <el-option label="范家" value="2"/>
            <el-option label="暗探" value="3"/>
            <el-option label="监察院" value="4"/>
          </el-select>
        </el-form-item>

        <el-form-item label="基本工资（单位：元）" required prop="baseSalary">
          <el-input
            v-model="employeeForm.baseSalary"
            style="width: 50%"
            type="number"
            placeholder="请输入基本工资"
          />
        </el-form-item>
        <el-form-item label="基本津贴（单位：元）" required prop="baseBonus">
          <el-input
            v-model="employeeForm.baseBonus"
            style="width: 50%"
            type="number"
            placeholder="请输入基本工资"
          />

        </el-form-item>

        <el-form-item label="状态" required prop="state">
          <el-select v-model="employeeForm.state" placeholder="请选择工作状态">
            <el-option label="在职" value="WORKING"/>
            <el-option label="退休" value="RETIRE"/>
            <el-option label="离职" value="QUIT"/>
          </el-select>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="employeeForm.phone" maxlength="11"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="employeeForm.email" maxlength="64"/>
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-input v-model="employeeForm.address" type="textarea" maxlength="128"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('employeeForm')">添加</el-button>
          <el-button type="danger" @click="resetForm">重置</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>/* eslint-disable indent */
import { isEmpty } from '../../../../utils/string'
import { addEmployee } from '../../../../api/employee'

export default {
  data() {
    return {
      employeeForm: {

        name: '',
        department: {
          departId: ''
        },
        departId: '',
        sex: '男',
        age: 0,
        state: 'WORKING',
        phone: '',
        email: '',
        address: '',
        baseSalary: 0,
        baseBonus: 0
      },
      rules: {
        name: [
          { required: true, message: '请输入雇员姓名', trigger: 'blur' },
          { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        departId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        baseSalary: [
          { required: true, message: '请输入正确的基本薪资', trigger: 'change' }
        ],
        baseBonus: [
          { required: true, message: '请输入正确的基本津贴', trigger: 'change' }
        ],
        state: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        phone: [
          { required: false, message: '', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的手机号格式', trigger: 'blur' }
        ],
        email: [
          { required: false, message: '', trigger: 'blur' },
          { min: 5, max: 32, message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        address: [
          { required: false, message: '', trigger: 'blur' },
          { min: 6, max: 128, message: '请输入正确的地址信息', trigger: 'blur' }
        ]
      }
    }
  },
  // mounted() {
  // },
  methods: {
    resetForm() {
      Object.assign(this.employeeForm, this.$options.data().form)
    },

    submitForm(formName) {
      const that = this
      console.log(this.$refs)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.employeeForm.department.departId = that.employeeForm.departId
          addEmployee(that.employeeForm).then(response => {
            if (response.success) {
              that.resetForm()
            }
            that.displayResult(response)
          })
        } else {
          return false
        }
      })
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
    displayResult(response) {
      if (!response.success) {
        this.$message({
          message: '添加出错！' + response.message,
          type: 'error'
        })
        return
      }
      this.$message({
        message: '添加成功!',
        type: 'success'
      })
      this.resetForm()
    }

  }
}
</script>

<style scoped>
  .line {
    text-align: center;
  }
</style>

